package com.leetcode.LC;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class LC387 {
    //思路：数组计数法，遍历数组找出第一个值为1的
    public int firstUniqChar(String s) {

        int[] ints = new int[26];
        Arrays.fill(ints,0);
        List<Character> list = new ArrayList<>();

        for (int i = 0; i < s.length(); i++) {
            ints[s.charAt(i)-'a']+=1;
        }
        for (int i = 0; i < ints.length; i++) {
            if (ints[i]==1){
                list.add((char) ('a'+i));
            }
        }

        for (int i = 0; i < s.length(); i++) {
            if (list.contains(s.charAt(i))){
                return i;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        System.out.println(new LC387().firstUniqChar("leetcode"));
    }
}
